#include "StdAfx.h"
#include "RibbonView.h"
#include "RibbonInfo.h"

CComModule _Module;


HRESULT CRibbonView::CreateFromResourceDLL(CComPtr<CRibbonView> pView, HWND hWnd, HINSTANCE hInstance, LPCTSTR szResourceDllName, LPCTSTR LPszResourceName)
{
	HMODULE hModule = LoadLibraryEx(szResourceDllName, NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_IGNORE_CODE_AUTHZ_LEVEL | LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
	if (hModule == NULL)
		return E_FAIL;

    HRESULT hr = Create(pView, hWnd, hInstance, hModule, LPszResourceName);
	
	return hr;
}

HRESULT CRibbonView::Create(CComPtr<CRibbonView> pView, HWND hWnd, HINSTANCE hInstance, HMODULE hModule, LPCTSTR LPszResourceName)
{
	if (hModule == NULL)
		return E_FAIL;
  
	IUIFramework *pUIFramework = NULL;
	::CoCreateInstance(CLSID_UIRibbonFramework, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pUIFramework));

    CComPtr<IUIApplication> spApplication(pView);
	pView->SetUIFramework(pUIFramework);
	pUIFramework->Initialize(hWnd, spApplication);
    HRESULT hResult = pUIFramework->LoadUI(hModule, LPszResourceName);
	ShowWindow(hWnd, SW_SHOWNORMAL);
	UpdateWindow(hWnd);

	pView->OnAfterCreateRibbon(hWnd, hInstance);
	return hResult;
}

HRESULT CRibbonView::SetUIFramework(IUIFramework *pUI)
{
	m_pUIFramework = pUI;
	return S_OK;
}

HRESULT CRibbonView::Destroy()
{
	HRESULT hResult = m_pUIFramework->Destroy();
	m_pUIFramework.Release();
	return hResult;
}
	
HRESULT CRibbonView::GetRibbonHeight(UINT32 *pHeight)
{
	CComPtr<IUIRibbon> ribbon;
	HRESULT hResult = m_pUIFramework->GetView(0, IID_PPV_ARGS(&ribbon));
	
	if (!SUCCEEDED(hResult))
		return hResult;

	return ribbon->GetHeight(pHeight);
}

HRESULT CRibbonView::OnViewChanged(UINT32 nViewID, __in UI_VIEWTYPE typeID, __in IUnknown* pView, UI_VIEWVERB verb, INT32 uReasonCode) 
{ 
	return S_OK;
}


HRESULT CRibbonView::OnCreateUICommand(UINT32 nCmdID, __in UI_COMMANDTYPE typeID, __deref_out IUICommandHandler** ppCommandHandler) 
{
	return E_NOTIMPL;  
}

HRESULT CRibbonView::OnDestroyUICommand(UINT32 commandId, __in UI_COMMANDTYPE typeID, __in_opt IUICommandHandler* pCommandHandler) 
{ 
    return E_NOTIMPL; 
}

HRESULT CRibbonView::Execute(UINT nCmdID,
		UI_EXECUTIONVERB verb, 
		__in_opt const PROPERTYKEY* key,
		__in_opt const PROPVARIANT* ppropvarValue,
		__in_opt IUISimplePropertySet* pCommandExecutionProperties)
{
	return S_OK;
}

HRESULT CRibbonView::UpdateProperty(UINT nCmdID,
		__in REFPROPERTYKEY key,
		__in_opt const PROPVARIANT* ppropvarCurrentValue,
		__out PROPVARIANT* ppropvarNewValue)
{
	return E_FAIL;
}
